package com.hula.onion.book.server.spider.platforms.yiju;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/30 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class YiJuSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(4);

        map.put("userName", form.getUserName());
        map.put("password", form.getPassword());
        map.put("code", form.getTicket());

        map.put("checked", "");
        map.put("checkLogin", 1);

        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {

        try {
            AccountInfo accountInfo = new AccountInfo();

            Document dc = Jsoup.parse(html);

            //总资产
            String amount = dc.selectFirst("div.ucmoney_left").children().get(1).children().get(1).text();

            Element ul = dc.selectFirst("div.ucinvestment").children().get(1);
            //待收利息
            String waitIntrest = ul.children().get(1).children().get(1).childNode(1).outerHtml();
            //累计投资收益
            String totalIntrest = ul.children().get(3).children().get(1).childNode(1).outerHtml();

            accountInfo.setAmount(new BigDecimal(amount));
            accountInfo.setWaitIntrest(new BigDecimal(waitIntrest));
            accountInfo.setTotalIntrest(new BigDecimal(totalIntrest));
            return new CommonResponse(accountInfo);
        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }
    }
}
